
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

public class AccountSystem {
    private static JLabel remainingLabel;
    private static JLabel expenseLabel;
    private static JLabel daysLeftLabel;
    private static JLabel todayExpenseLabel;

    Calendar calendar = Calendar.getInstance();                             // 获取当前日期
    int currentDay = calendar.get(Calendar.DAY_OF_MONTH);                   // 今天是这个月的第几天
    int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);    // 现在月份的最大天数
    int daysLeft = daysInMonth - currentDay;                               // 计算距离本月末的天数


    public static void main(String[] args) {

        AccountSystem accountSystem = new AccountSystem();
        accountSystem.FirstWindow();
    }

    public void FirstWindow() {

        // 创建第一个窗口
        JFrame firstFrame = new JFrame("记账系统");
        firstFrame.setBounds(480, 300, 600, 300);
        firstFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 顶层面板和底层面板
        JPanel topPanel = new JPanel(new GridLayout(2, 2));
        JPanel bottomPanel = new JPanel();

        // 创建四个label
        remainingLabel = new JLabel("本月剩余：0");
        remainingLabel.setFont(new Font("宋体", Font.PLAIN, 30));
        daysLeftLabel = new JLabel("距离本月末天数：" + daysLeft);
        daysLeftLabel.setFont(new Font("宋体", Font.PLAIN, 30));
        expenseLabel = new JLabel("本月消费：0");
        expenseLabel.setFont(new Font("宋体", Font.PLAIN, 30));
        todayExpenseLabel = new JLabel("今日消费金额：0");
        todayExpenseLabel.setFont(new Font("宋体", Font.PLAIN, 30));

        // 按钮“记”
        JButton recordButton = new JButton("记");
        recordButton.setPreferredSize(new Dimension(120, 120));
        String text = recordButton.getText(); // 获取按钮上的文本
        recordButton.setFont(new Font("宋体", Font.PLAIN, 40));
        recordButton.setText(text);

        recordButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                SecondWindow();
            }
        });

        firstFrame.setLayout(new BorderLayout());

        // 将组件添加到主窗口
        topPanel.add(remainingLabel);
        topPanel.add(daysLeftLabel);
        topPanel.add(expenseLabel);
        topPanel.add(todayExpenseLabel);
        bottomPanel.add(recordButton);

        // 将顶层面板和底层面板添加到边界布局中
        firstFrame.add(topPanel, BorderLayout.CENTER);
        firstFrame.add(bottomPanel, BorderLayout.SOUTH);

        // 显示主窗口
        firstFrame.setVisible(true);
    }

    private static void SecondWindow() {

        // 创建第二个窗口
        JFrame secondFrame = new JFrame("记账");
        secondFrame.setBounds(450, 300, 600, 300);

        JPanel panel = new JPanel(new GridLayout(1, 2));

        // 创建两个按钮
        JButton incomeButton = new JButton("收入");
        incomeButton.setPreferredSize(new Dimension(120, 70));
        String text1 = incomeButton.getText(); // 获取按钮上的文本
        incomeButton.setFont(new Font("宋体", Font.PLAIN, 20));
        incomeButton.setText(text1);

        incomeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                Third1Window();
            }
        });

        JButton expenseButton = new JButton("支出");
        expenseButton.setPreferredSize(new Dimension(120, 70));
        String text2 = expenseButton.getText(); // 获取按钮上的文本
        expenseButton.setFont(new Font("宋体", Font.PLAIN, 20));
        expenseButton.setText(text2);

        expenseButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                Third2Window();
            }
        });

        panel.add(incomeButton);
        panel.add(expenseButton);

        secondFrame.add(panel, BorderLayout.CENTER);

        // 显示第二个窗口
        secondFrame.setVisible(true);
    }

    private static void Third1Window() {

        // 创建第三个窗口
        JFrame third1Frame = new JFrame("收入");
        third1Frame.setBounds(450, 300, 600, 300);
        third1Frame.setLayout(new BorderLayout());
        third1Frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel topPanel1 = new JPanel(new GridLayout(2, 1));
        JPanel bottomPanel1 = new JPanel();

        // 创建组件
        JLabel incomeAmountLabel = new JLabel("收入金额：");
        incomeAmountLabel.setFont(new Font("宋体", Font.PLAIN, 20));
        JTextField incomeAmountField = new JTextField();

        JLabel incomeReasonLabel = new JLabel("收入原因：");
        incomeReasonLabel.setFont(new Font("宋体", Font.PLAIN, 20));
        JTextField incomeReasonField = new JTextField();

        JButton nextButton = new JButton("下一步");
        nextButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // 更新主窗口数据
                double incomeAmount = Double.parseDouble(incomeAmountField.getText());

                updateMainWindowData1(incomeAmount);

                // 打开第四个窗口
                FourthWindow(incomeAmount, incomeReasonField.getText());

                // 关闭第三个窗口
                third1Frame.dispose();
            }
        });

        // 将组件添加到第三个窗口
        topPanel1.add(incomeAmountLabel);
        topPanel1.add(incomeAmountField);
        topPanel1.add(incomeReasonLabel);
        topPanel1.add(incomeReasonField);

        bottomPanel1.add(nextButton);

        third1Frame.add(topPanel1, BorderLayout.CENTER);
        third1Frame.add(bottomPanel1, BorderLayout.SOUTH);

        // 显示第三个窗口
        third1Frame.setVisible(true);
    }

    private static void Third2Window() {

        // 创建第三个窗口
        JFrame third2Frame = new JFrame("记账");
        third2Frame.setBounds(450, 300, 600, 300);
        third2Frame.setLayout(new BorderLayout());
        third2Frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel topPanel1 = new JPanel(new GridLayout(2, 1));
        JPanel bottomPanel1 = new JPanel();

        // 创建组件
        JLabel expenseAmountLabel = new JLabel("支出金额：");
        expenseAmountLabel.setFont(new Font("宋体", Font.PLAIN, 20));
        JTextField expenseAmountField = new JTextField();

        JLabel expenseReasonLabel = new JLabel("支出原因：");
        expenseReasonLabel.setFont(new Font("宋体", Font.PLAIN, 20));
        JTextField expenseReasonField = new JTextField();

        JButton nextButton = new JButton("下一步");
        nextButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // 更新主窗口数据
                double expenseAmount = Double.parseDouble(expenseAmountField.getText());
                updateMainWindowData2(expenseAmount);

                // 打开第四个窗口
                FourthWindow(expenseAmount, expenseReasonField.getText());

                // 关闭第三个窗口
                third2Frame.dispose();
            }
        });

        // 将组件添加到第三个窗口
        topPanel1.add(expenseAmountLabel);
        topPanel1.add(expenseAmountField);
        topPanel1.add(expenseReasonLabel);
        topPanel1.add(expenseReasonField);
        bottomPanel1.add(nextButton);

        third2Frame.add(topPanel1, BorderLayout.CENTER);
        third2Frame.add(bottomPanel1, BorderLayout.SOUTH);

        // 显示第三个窗口
        third2Frame.setVisible(true);
    }

    private static void FourthWindow(double amount, String reason) {
        // 创建第四个窗口
        JFrame fourthFrame = new JFrame("确认");
        fourthFrame.setBounds(450, 300, 600, 300);
        fourthFrame.setLayout(new BorderLayout());
        fourthFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel topPanel = new JPanel(new GridLayout(2, 1));
        JPanel bottomPanel = new JPanel();

        // 创建组件
        JLabel amountLabel = new JLabel("金额： " + amount);
        amountLabel.setFont(new Font("宋体", Font.PLAIN, 20));

        JLabel reasonLabel = new JLabel("原因： " + reason);
        reasonLabel.setFont(new Font("宋体", Font.PLAIN, 20));

        JButton confirmButton = new JButton("确认");
        confirmButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // 更新主窗口数据
                updateMainWindowData3(amount);

                // 关闭第四个窗口
                fourthFrame.dispose();
            }
        });

        // 将组件添加到第四个窗口
        topPanel.add(amountLabel);
        topPanel.add(reasonLabel);
        bottomPanel.add(confirmButton);

        fourthFrame.add(topPanel, BorderLayout.CENTER);
        fourthFrame.add(bottomPanel, BorderLayout.SOUTH);

        // 显示第四个窗口
        fourthFrame.setVisible(true);
    }

    private static void updateMainWindowData1(double incomeAmount) {
        // 更新主窗口数据
        double remaining = Double.parseDouble(remainingLabel.getText().substring(5));
        double expense = Double.parseDouble(expenseLabel.getText().substring(4));
        double todayExpense = Double.parseDouble(todayExpenseLabel.getText().substring(7));

        remaining += incomeAmount;
        expense += incomeAmount;
        todayExpense += incomeAmount;

        remainingLabel.setText("本月剩余：" + remaining);
        expenseLabel.setText("本月消费：" + expense);
        todayExpenseLabel.setText("今日消费金额：" + todayExpense);
    }

    private static void updateMainWindowData2(double expenseAmount) {
        // 更新主窗口数据
        double remaining = Double.parseDouble(remainingLabel.getText().substring(5));
        double expense = Double.parseDouble(expenseLabel.getText().substring(4));
        double todayExpense = Double.parseDouble(todayExpenseLabel.getText().substring(7));

        remaining -= expenseAmount;
        expense += expenseAmount;
        todayExpense += expenseAmount;

        remainingLabel.setText("本月剩余：" + remaining);
        expenseLabel.setText("本月消费：" + expense);
        todayExpenseLabel.setText("今日消费金额：" + todayExpense);
    }

    private static void updateMainWindowData3(double amount) {
        // 更新主窗口数据
        double remaining = Double.parseDouble(remainingLabel.getText().substring(5));
        double expense = Double.parseDouble(expenseLabel.getText().substring(4));
        double todayExpense = Double.parseDouble(todayExpenseLabel.getText().substring(7));

        remaining -= amount;
        expense += amount;
        todayExpense += amount;

        remainingLabel.setText("本月剩余：" + remaining);
        expenseLabel.setText("本月消费：" + expense);
        todayExpenseLabel.setText("今日消费金额：" + todayExpense);
    }
}
